package offer.algorithm;

/**
 * Description：
 * Author: zhangc
 * Date：2017/7/28 14:39
 */
public class RegularMatch53 {

    public static boolean isMatch(String src, String regular) {
        int srcLength = src.length();
        int regularLength = regular.length();
        int i = 0, j = 0;
        while (i < srcLength && j < regularLength) {
            if (src.charAt(i) == regular.charAt(j) || regular.charAt(j) == '.') {
                ++i;
                ++j;
            } else if (src.charAt(i) != regular.charAt(j)) {
                if (j + 1 == regularLength)
                    return false;
                else if (regular.charAt(j) != '*') {
                    while (j + 1 < regularLength && regular.charAt(j + 1) == regular.charAt(j))
                        j++;
                    if (j + 1 == regularLength || regular.charAt(j + 1) != '*')
                        return false;
                    else j += 2;
                }
            } else {
                while (i + 1 < srcLength && src.charAt(i + 1) == src.charAt(i))
                    ++i;
                j++;
            }
        }
        if (i == srcLength && j == regularLength)
            return true;
        return false;
    }

}
